package com.example.controller;


import com.example.common.Page;
import com.example.common.Result;
import com.example.common.Spart;
import com.example.common.StorePart;
import com.example.entity.Part;
import com.example.service.PartsService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@RestController
@RequestMapping("/parts")
public class PartsController {

    @Resource
    private PartsService partsService;

    // 搜索零件
    @GetMapping("/search")
    public Result<Page<List<Spart>>> searchParts(@RequestParam(required = false) String pid,
                              @RequestParam(required = false) String name
                              ) {

        List<Spart> spartList = null;
        Pattern pattern = Pattern.compile("[0-9]{1,}");
        Matcher matcher = pattern.matcher((CharSequence) pid);

        //pid为数值
        if(matcher.matches()) {
            spartList = partsService.findPartsByPid(Long.parseLong(pid));
        }
        //无pid，用名字模糊查询
        else if(!name.equals("undefined")){
            spartList = partsService.findPartsByName(name);
        }
        //没定义，查询所有的
        else if(pid.equals("undefined")){
            spartList = partsService.findAllParts();
        }
        //System.out.print("pid is " + pid.toString() + " and name is " + name + "\n");



        Page<List<Spart>> page = new Page<>();
        page.setContent(spartList);
        return Result.success(page);

    }

    @GetMapping("/container")
    public Result<Page<List<Spart>>> searchContainer(@RequestParam(required = false) String pid,
                                                 @RequestParam(required = false) String name
    ) {

        List<Spart> spartList = null;
        Pattern pattern = Pattern.compile("[0-9]{1,}");
        Matcher matcher = pattern.matcher((CharSequence) pid);

        //pid为数值
        if(matcher.matches()) {
            spartList = partsService.findContainersByPid(Long.parseLong(pid));
        }
        //无pid，用名字模糊查询
        else if(!name.equals("undefined")){
            spartList = partsService.findContainersByName(name);
        }
        //没定义，查询所有的
        else if(pid.equals("undefined")){
            spartList = partsService.findAllContainers();
        }
        //System.out.print("pid is " + pid.toString() + " and name is " + name + "\n");



        Page<List<Spart>> page = new Page<>();
        page.setContent(spartList);
        return Result.success(page);

    }

    @PostMapping
    public Result add(@RequestBody Part part) {
        partsService.save(part);
        return Result.success();
    }

    @GetMapping("/page")
    public Result<Page<List<StorePart>>> findPage(@RequestParam(required = false) Long store_pid,
                                                  @RequestParam(required = false) Long store_container_pid,
                                                  @RequestParam(required = false) String part_name_f) {

        if(part_name_f == null){
            part_name_f = "undefined";
        }
        if(store_pid == null){
            store_pid = Long.parseLong("-1");
        }
        if(store_container_pid == null){
            store_container_pid = Long.parseLong("-1");
        }
        Page<List<StorePart>> page = new Page<>();
        List<StorePart> spartList = null;
        StorePart storePart = new StorePart();
        //如果零件编号和容器编号都有，则通过两个参数来查询
        if((store_pid > 0) & (store_container_pid > 0)){
            storePart.setStore_pid(store_pid);
            storePart.setStore_container_pid(store_container_pid);

            spartList = partsService.findStoreByPidCid(store_pid, store_container_pid);
            page.setContent(spartList);

        }
        //单有零件编号，根据零件编号查询
        else if(store_pid > 0){
            storePart.setStore_pid(store_pid);

            spartList = partsService.findStoreByPid(store_pid);
            page.setContent(spartList);

        }
        //单有容器编号
        else if(store_container_pid > 0){
            storePart.setStore_container_pid(store_container_pid);

            spartList = partsService.findStoreByCid(store_container_pid);
            page.setContent(spartList);

        }
        //模糊查询
        else if(!part_name_f.equals("undefined")){
            spartList = partsService.findStoreFuzzy(part_name_f);
            page.setContent(spartList);
        }
        //其它情况下，查询全部
        else {
            spartList = partsService.findAllStore();
            page.setContent(spartList);
        }

        //如果只有零件编号

        return Result.success(page);
    }

    @PostMapping("/store")
    public Result store(@RequestBody StorePart storePart) {
        //判断参数是否正确，如果不正确返回错误，如果正确将数据插入库存表。
        if((storePart.getStore_pid() > 0) & (storePart.getStore_container_pid() > 0) & (storePart.getStore_quality() > 0)){
            partsService.storeParts(storePart);

            return Result.success();
        }
        else {
            return Result.error("403","参数错误");
        }
    }

    @PostMapping("/adjstore")
    public Result<Page<List<StorePart>>> adjstore(@RequestBody StorePart storePart) {
        Page<List<StorePart>> page = new Page<>();
        List<StorePart> spartList = null;

        partsService.adjStore(storePart.getStore_rid(), storePart.getStore_quality());

        spartList = partsService.findStoreByRid(storePart.getStore_rid());
        page.setContent(spartList);

        return Result.success(page);
    }

}
